<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Car;
use App\Services\OSS;
use DB;

class CarController extends Controller
{
    /**
     * 官网首页
     */
    public function index() {
    	$car = new Car;
        // 获取所有的汽车品牌
    	$brand = array_column($car->GetBrand()->toArray(),'brand');
        // 首页数据
    	$cars = $car->HomePage();
        // 图片位置
        $urlcar = OSS::getPublicObjectURL(config('aliyun.BucketName'),'Image/Cars/');
    	return view('carshow.index',
            [
                'brand'=>$brand,
                'cars'=>$cars,
                'carurl'=>$urlcar
            ]
        );
    }

    /**
     * 新车列表
     */
    public function NewCars(Request $request) {
        // 通过条件读取响应的数据
    	$car = new Car;
    	$screen['brand'] = ($request->brand);
    	$screen['searchkey'] = $request->searchkey;
        // 排序
    	$screen['guide_price'] = strstr($request->price,'-')?explode('-',$request->price):$request->price;
    	$screen['first_pay'] = strstr($request->payments,'-')?explode('-',$request->payments):$request->payments;
    	$screen['month_pay'] = strstr($request->MonthPay,'-')?explode('-',$request->MonthPay):$request->MonthPay;
        $screen['order'] = $request->input('order','preferentialRatio');
        // 筛选
    	$screen['Models'] = $request->Models;
    	$screen['region_id'] = $request->region;
        // 调用model方法
    	$data = $car->CarScreen($screen);
        // 获取所有品牌列表
    	$brand = array_column($car->GetBrand()->toArray(),'brand');
        // 获取图片的位置
        $urlcar = OSS::getPublicObjectURL(config('aliyun.BucketName'),'Image/Cars/');
    	return view('carshow.newcar',
        [
            'cars'=>$data,
            'brand'=>$brand,
            'request'=>$request,
            'carurl'=>$urlcar
        ]);
    }   

    /**
     * 汽车详情
     */
    public function details(Request $request) {
        // 读取汽车数据
        $cardata = Car::find($request->carid);
        $cardetails = DB::table('jdc_car_details')->where('carnum','=',$cardata->carnum)->first();
        // 个别json格式的数据转为数组
        $json_de = [
            'base_info','config_spotlight',
            'models_spotlight','body_config',
            'engine_config','gearbox_config',
            'chassis_steering','wheel_brake',
            'safety_equip','control_config',
            'outside_config','inside_config',
            'chair_config','multimedia_config',
            'light_config','mirrors_config',
            'aircondition_config','hightech_config'
        ];
        foreach ($cardetails as $key => $value) {
            if(in_array($key,$json_de)){
               $cardetails->$key = json_decode($value,true);
            }
        }
        // 查询汽车价格情况
        $carprice = DB::table('jdc_car_price')->where('carnum','=',$cardata->carnum)->get();
        $ted_cars = DB::table('jdc_cars')->whereIn('id',['7','11','14','19'])->get();
        // 读取oss图片
        $carurl = OSS::getPublicObjectURL(config('aliyun.BucketName'),'Image/Cars/'.$cardata['carnum']);
        $carurl_light = OSS::getPublicObjectURL(config('aliyun.BucketName'),'Image/CarsLight/'.$cardata['carnum']);
        $urlcar = OSS::getPublicObjectURL(config('aliyun.BucketName'),'Image/Cars/');
        // 返回视图
        return view('carshow.details',[
            'carurl'=>$carurl,
            'car'=>$cardata,
            'cardetails'=>$cardetails,
            'carurl_light'=>$carurl_light,
            'carted'=>$ted_cars,
            'urlcar'=>$urlcar,
            'carprice'=>$carprice
        ]);
    }

    /**
     * 了解筋斗车
     */
    public function Company(Request $request) {
        return view('carshow.company');
    }

    /**
     * 筋斗问答
     */
    public function FAQ() {
        return view('index.problem');
    }
}
